package com.lisp.clp.child_first;

import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.lisp.clp.box.IBox;

public class RightUse {
	public static void main(String[] args) throws Exception {
		
		Log log = LogFactory.getLog(RightUse.class); // 由于pom没有依赖log4j, 这里会使用java.util.logging
		log.info("before loading module");
		
		ChildFirstClassLoader childClassLoader = new ChildFirstClassLoader(
				new URL[] {
						new URL("file:lib/log4j-1.2.17.jar"),
						new URL("file:lib/commons-logging-1.0.4.jar"),
						new URL("file:lib/"),           // 用于加载log4j.xml
						new URL("file:target/classes/") // 用于加载com.lisp.clp.box.impl.BoxImplWithJCL
				});
		Thread.currentThread().setContextClassLoader(childClassLoader);
	    loadModule(childClassLoader);  // loadModule前设置TCCL, 之后要恢复TCCL
	    Thread.currentThread().setContextClassLoader(RightUse.class.getClassLoader());
	    
	    log.info("after loading module");
	}

	private static void loadModule(ChildFirstClassLoader childClassLoader)
			throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		Class<?> boxClass = childClassLoader.loadClass("com.lisp.clp.box.impl.BoxImplWithJCL");
	    IBox box = (IBox) boxClass.newInstance();
	    box.print();
	}
}
